/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.util.Collections;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import socof.wget.SOCOFWget;

/**
 *
 * @author Carlos Pinheiro
 */
public class UIUpdater extends Thread
{
    private static UIUpdater uu = new UIUpdater();
    
    private boolean monitorProcess = true;
    private JTextField numberDownloadersTxt;
    private JTextField numberParsersTxt;
    private JTextPane urlsDownloaded;
    private JTextPane urlsParsed;
    
    
    public static UIUpdater getInstance()
    {
        return uu;
    }
    
    public void setFields (JTextField numberDownloadersTxt, JTextField numberParsersTxt,
                            JTextPane urlsDownloaded, JTextPane urlsParsed)
    {
        this.numberDownloadersTxt = numberDownloadersTxt;
        this.numberParsersTxt = numberParsersTxt;
        this.urlsDownloaded = urlsDownloaded;
        this.urlsParsed = urlsParsed;
    }
    
    @Override
    public void run()
    {
        int executingDownloaders;
        int executingParsers;
        while (monitorProcess)
        {
            executingDownloaders = SOCOFWget.getNumberExecutingDownloaders();
            executingParsers = SOCOFWget.getNumberExecutingParsers();
            
            StringBuilder sb = new StringBuilder();
            for(String s : SOCOFWget.getUrlsDownloaded())
            {
                sb.append(s);
                sb.append("\n");
            }
            
            StringBuilder sb1 = new StringBuilder();
            for(String s : SOCOFWget.getFilesParsed())
            {
                sb1.append(s);
                sb1.append("\n");
            }
            
            urlsDownloaded.setText(sb.toString());
            urlsParsed.setText(sb1.toString());
            
            
            numberDownloadersTxt.setText("" + executingDownloaders);
            numberParsersTxt.setText("" + executingParsers);            
            
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException ex)
            {
                //Logger.getLogger(Grafica.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
